<?php
session_start();
if ($_SESSION['user'] == "") {
    ?>
    <script>
        alert('当前未登录！');
        window.location.href = '../login.html';
    </script>
    <?php
}
try {
    ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改用户信息</title>
    <link href="../css/regin.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
$db = new PDO('mysql:dbname=liaotian;host=106.14.7.147','root','123456');
$add= $db->prepare('select username,nicheng,userImg,sex from user WHERE username=:username');
$data[':username']=$_SESSION['user'];

if($add->execute($data) === false){
    print_r($add->errorInfo());
}
$re=$add->fetch();
    ?>
    <form id="myForm" action="../action/updateUserAction.php" method="post" enctype="multipart/form-data">
        <div id="regin">
            <div id="biaoti">
                <span>修改用户信息</span>
            </div>
            <div id="user">
                <div class="user_1">账号：</div>
                <div class="user_2">
                    <?="$re[username]"; ?>
                </div>
            </div>
            <div id="password">
                <div class="pwd_1">密码：</div>
                <div class="pwd_2">
                    <input name="password" type="password">
                    <span id="message"></span>
                </div>
            </div>
            <div id="repassword">
                <div class="repwd_1">重复密码：</div>
                <div class="repwd_2">
                    <input name="repassword" type="password">
                </div>
            </div>
            <div id="nicheng">
                <div class="nicheng_1">昵称：</div>
                <div class="nicheng_2">
                    <input name="nicheng" type="text" value="<?="$re[nicheng]"; ?>">
                </div>
            </div>
            <div id="sex">
                <div class="sex_1">性别：</div>
                <div class="sex_2">
                    <input name="sex" type="radio" value="男"  <?php echo $re['sex'] ?"checked='checked'":"";?> /><span>男</span>
                    <input name="sex" type="radio" value="女"  <?php echo $re['sex'] ?"":"checked='checked'";?> /><span>女</span>
                </div>
            </div>
            <div id="userImg">
                <div class="userImg_1">头像：</div>
                <div class="userImg_2">
                    <div class="userImg_1_img">
                        <img src="<?="$re[userImg]"; ?>" />
                    </div>
                    <input name="userImg" id="file" type="file">
                </div>
            </div>
            <div id="login">
                <input id="submit" type="submit" value="保存">
                <input type="button" onclick="window.location.href='room.php'" value="退出">
            </div>
        </div>
    </form>
    <?php
} catch (PDOException $e) {
    echo 'Connection failed';
}
?>
<script>
    var username='<?php echo $_SESSION['user']; ?>';
    var submitButton=document.getElementById('submit');
    var messageElement=document.getElementById('message');
    var myFormElement=document.getElementById('myForm');
    submitButton.onclick=function(e) {
        e.preventDefault();
        var passwordElement = document.querySelector('#myForm input[name=password]');
        var password = passwordElement.value;
        if (password == "") {
            messageElement.innerHTML = '密码不能为空';
            return;
        };
        var repasswordElement = document.querySelector('#myForm input[name=repassword]');
        var repassword = repasswordElement.value;
        if (repassword == "") {
            messageElement.innerHTML = '重复密码不能为空';
            return;
        }
        if(repassword !=password ){
            messageElement.innerHTML = '密码不一致！';
        };
        var nichengElement = document.querySelector('#myForm input[name=nicheng]');
        var nicheng = nichengElement.value;
        if (nicheng == "") {
            messageElement.innerHTML = '昵称不能为空';
            return;
        };
        var sexElement = document.querySelector('#myForm input[name=sex]');
        var sex = sexElement.value;
        if (sex == "") {
            messageElement.innerHTML = '性别不能为空';
            return;
        };
        var userImgElement = document.querySelector('#myForm input[name=userImg]');
        var userImg = userImgElement.value;
        if (userImg == "") {
            messageElement.innerHTML = '头像不能为空';
            return;
        };
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (this.readyState == 4) {
                if (this.status == 200) {
                    var result = JSON.parse(this.responseText);
                    if (result.error == 1) {
                        messageElement.innerHTML = result.message;
                    } else {
                        alert("修改用户信息成功！")
                        location.href = 'room.php';
                    }
                } else {
                    alert("异常");
                }
            }
        };
        xhr.open(myFormElement.method, myFormElement.action);
        var data = new FormData();
        data.append('username',username);
        data.append('password', password);
        data.append('repassword', repassword);
        data.append('nicheng', nicheng);
        data.append('sex', sex);
        data.append('userImg', userImgElement.files[0]);
        xhr.send(data);
    }

</script>
</body>
</html>